<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>倒计时用于活动秒杀</title>
    <style>
 
        *{
     
          margin: 0;
     
          padding:0;
     
        }
     
        p {
     
          font-size: 20px;
     
          text-align: center;
     
        }
     
        p span {
     
          color: red;
     
        }
     
        p span.time {
     
          color: black
     
        }
     
        body{padding-top:200px;}
     
      </style>
    
</head>
<body>
    <p>距离<d id="div"></d>还有</p>
 
    <p><span></span></p>

    <script>
        
        var oSpan = document.getElementsByTagName('span')[0];
        var div = document.getElementById('div');
        // 封装倒计时函数
        function countDown(time){
           div.innerText = time;
            var nowTime = new Date();//当前时间总的毫秒数
            var inputTime = new Date(time);//输入时间的毫秒数
            var times =  Math.floor((inputTime-nowTime)/1000);
            if(times==0 && times<= 0){
                clearInterval(goTOTime);
            }
            var day =timeNew(parseInt(times/60/60/24));//计算出距离结束还有多少天
            var hour =timeNew(parseInt(times/60/60%24));//计算出距离结束还有多少小时
            var minute =timeNew(parseInt(times/60%60));//分钟
            var second =timeNew(parseInt(times%60));//秒
            // return d +'天'+h+'时'+m+'分'+s+'秒';
            //数据渲染到元素
            var str = day + '<span class="time">天</span>'

                + hour+ '<span class="time">小时</span>'
        
                + minute+ '<span class="time">分钟</span>'
        
                + second+ '<span class="time">秒</span>';
        
               oSpan.innerHTML = str;
        }
        function timeNew(time){
        return time = time<10 ?'0'+ time:time;
        }
        //传当前时间数转换成毫秒数进行计算
        var goTOTime  = null;
                goTOTime =  setInterval(function(){
                    countDown('2020-5-12 01:10:00')
            },1000);
            // clearTimeout(goTOTime);
    </script>
</body>
</html>